﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundDetailServiceRef;

    public class ManualPutaway : BasePage
    {
        protected Button btnGoBack;
        protected Button btnPutAway;
        protected Button btnSave;
        protected HtmlForm form1;
        protected GridView gvManualPutAway;
        protected HtmlInputHidden hdnQty;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblFormatExample;
        protected Label lblLocID;
        protected Label lblManualPA;
        protected Label lblMsg;
        protected Label lblQty;
        protected Label lblSkuID;
        protected Label lblUomID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected ScriptManager ScriptManager1;
        protected TextBox txtLocID;
        protected TextBox txtQty;
        protected TextBox txtSkuID;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            this.hdnRtnValue.Value = "3,0";
        }

        protected void btnPutAway_Click(object sender, EventArgs e)
        {
            try
            {
                this.lblMsg.Visible = false;
                if (string.IsNullOrEmpty(this.txtLocID.Text.Trim()))
                {
                    this.lblMsg.Text = "请输入货位!";
                    this.lblMsg.Visible = true;
                }
                else if (BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.txtLocID.Text.Trim(), "LOCATION") == null)
                {
                    base.ShowMsgLabel(this.lblMsg, "请输入有效货位!");
                    this.txtLocID.Text = "";
                    this.txtLocID.Focus();
                }
                else if (string.IsNullOrEmpty(this.txtQty.Text.Trim()))
                {
                    this.lblMsg.Text = "请输入上架数量!";
                    this.lblMsg.Visible = true;
                }
                else if (decimal.Parse(this.txtQty.Text.Trim()) <= 0M)
                {
                    this.lblMsg.Text = "请输入大于0的上架数量!";
                    this.lblMsg.Visible = true;
                }
                else if (decimal.Parse(this.txtQty.Text.Trim()) > decimal.Parse(this.hdnQty.Value))
                {
                    this.lblMsg.Text = "上架数量不能大于剩余数量!";
                    this.lblMsg.Visible = true;
                }
                else
                {
                    List<ReceiptPutawayInfo> receiptPutawayInfo = null;
                    if (this.Session["ManualPutAway"] != null)
                    {
                        receiptPutawayInfo = this.Session["ManualPutAway"] as List<ReceiptPutawayInfo>;
                    }
                    else
                    {
                        receiptPutawayInfo = new List<ReceiptPutawayInfo>();
                    }
                    Receiving.PutawayParas putaway = this.Session["PutAwayParas"] as Receiving.PutawayParas;
                    for (int i = 0; i < receiptPutawayInfo.Count; i++)
                    {
                        if (receiptPutawayInfo[i].ToLoc == this.txtLocID.Text.Trim())
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F4200_029");
                            this.lblMsg.Visible = true;
                            return;
                        }
                    }
                    ReceiptPutawayInfo receiptPutaway = new ReceiptPutawayInfo {
                        TransID = putaway.TransID,
                        ReceiptID = putaway.ReceiptID,
                        ReceiptLineID = putaway.ReceiptLineID,
                        SkuID = putaway.SkuID,
                        FromLoc = putaway.FromLoc,
                        ToLoc = this.txtLocID.Text.Trim(),
                        Qty = Convert.ToDecimal(this.txtQty.Text.Trim()),
                        OwnerID = putaway.OwnerID,
                        Lpn = putaway.LpnID,
                        CondititonCode = putaway.ConditionCode,
                        Uom = putaway.UomID,
                        Lot = putaway.LotID,
                        PackID = putaway.PackID,
                        UpdatedBy = base.UserInfo.UpdatedBy
                    };
                    receiptPutawayInfo.Add(receiptPutaway);
                    this.Session["ManualPutAway"] = receiptPutawayInfo;
                    decimal remainQty = Convert.ToDecimal(this.hdnQty.Value) - Convert.ToDecimal(this.txtQty.Text.Trim());
                    this.txtQty.Text = base.GetPreferenceDigit(remainQty);
                    this.hdnQty.Value = base.GetPreferenceDigit(remainQty);
                    base.BindData2GridView(this.gvManualPutAway, receiptPutawayInfo);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Session["ManualPutAway"] != null)
                {
                    List<ReceiptPutawayInfo> receiptPutawayInfo = this.Session["ManualPutAway"] as List<ReceiptPutawayInfo>;
                    if ((receiptPutawayInfo != null) && (receiptPutawayInfo.Count > 0))
                    {
                        InboundDetailServiceClientProxy.GetInstance().GenPutAway(receiptPutawayInfo);
                        this.Session["ManualPutAway"] = null;
                        this.hdnRtnValue.Value = "3";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void gvManualPutAway_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void gvManualPutAway_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<ReceiptPutawayInfo> receiptPutawayInfo = null;
            if (this.Session["ManualPutAway"] != null)
            {
                receiptPutawayInfo = this.Session["ManualPutAway"] as List<ReceiptPutawayInfo>;
                decimal remainQty = Convert.ToDecimal(this.hdnQty.Value) + Convert.ToDecimal(receiptPutawayInfo[e.RowIndex].Qty);
                this.txtQty.Text = base.GetPreferenceDigit(remainQty);
                this.hdnQty.Value = base.GetPreferenceDigit(remainQty);
                receiptPutawayInfo.RemoveAt(e.RowIndex);
                this.Session["ManualPutAway"] = receiptPutawayInfo;
                if (receiptPutawayInfo.Count == 0)
                {
                    this.ShowGridViewHeader();
                }
                else
                {
                    base.BindData2GridView(this.gvManualPutAway, receiptPutawayInfo);
                }
            }
        }

        private void LoadData()
        {
            if (this.Session["PutAwayParas"] != null)
            {
                Receiving.PutawayParas putaway = this.Session["PutAwayParas"] as Receiving.PutawayParas;
                this.txtSkuID.Text = putaway.SkuID;
                this.txtQty.Text = base.GetPreferenceDigit(putaway.Qty);
                this.lblUomID.Text = putaway.UomID;
                this.hdnQty.Value = base.GetPreferenceDigit(putaway.Qty);
                this.btnSave.Enabled = string.Compare(putaway.Status, "closed", true) != 0;
                List<ReceiptPutawayInfo> receiptPutAwayInfos = InboundDetailServiceClientProxy.GetInstance().GetPendingPutAwayTaskByTransID(putaway.TransID);
                if ((receiptPutAwayInfos != null) && (receiptPutAwayInfos.Count > 0))
                {
                    this.Session["ManualPutAway"] = receiptPutAwayInfos;
                    base.BindData2GridView(this.gvManualPutAway, receiptPutAwayInfos);
                    decimal pendingQty = 0M;
                    for (int i = 0; i < receiptPutAwayInfos.Count; i++)
                    {
                        pendingQty += receiptPutAwayInfos[i].Qty;
                    }
                    this.txtQty.Text = base.GetPreferenceDigit((decimal) (putaway.Qty - pendingQty));
                    this.hdnQty.Value = base.GetPreferenceDigit((decimal) (putaway.Qty - pendingQty));
                }
                else
                {
                    this.Session["ManualPutAway"] = null;
                    this.ShowGridViewHeader();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            base.Response.Expires = -1;
            this.lblMsg.Text = "";
        }

        private void ShowGridViewHeader()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ToLoc");
            dt.Columns.Add("Qty");
            dt.Columns.Add("Uom");
            dt.Columns.Add("CondititonCode");
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            base.BindData2GridView(this.gvManualPutAway, dt);
            this.gvManualPutAway.Rows[0].Cells[1].Text = "";
            this.gvManualPutAway.Rows[0].Cells[0].Controls.Clear();
        }
    }
}

